<template>
  <div class="com-container">
    <div class="title">
      <el-select
        v-model="selectData"
        placeholder="请选择"
        :size="size"
        @change="selchange(selectData)"
      >
        <el-option
          v-for="item in typeData"
          :key="item.index"
          :label="item.text"
          :value="item.key"
        >
        </el-option>
      </el-select>
    </div>
    <div class="com-chart" ref="trend"></div>
  </div>
</template>

<script>
import "../assets/js/essos.js";
export default {
  data() {
    return {
      allData: [],
      myCharts: null,
      selectData: "",
      typeData: [],
      size: "",
    };
  },
  methods: {
    //初始化报表
    async initCharts() {
      this.myCharts = this.$echarts.init(this.$refs.trend, "essos");
      //初始化报表配置项，与数据无关
      const initOptions = {
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
      };
      this.myCharts.setOption(initOptions);
    },
    //更改选项的数据
    selchange() {
      this.createReports();
    },
    seldata() {},
    //创建报表
    async createReports() {
      //获取数据
      const { data } = await this.$axios.get("http://localhost:3000/trender");
      const mapData = data.map.data;
      this.typeData = data.type;
      // 判断当选项有值时，this.selectData为当前值；如果没有输入值时，this.selectData默认为this.typeData的第一个key值
      if (this.selectData != "") {
        this.selectData;
      } else {
        this.selectData = this.typeData[0].key;
      }
      let title = "";
      //根据当前选项的值更改图表的数据
      if (this.selectData == "map") {
        this.allData = mapData;
        title = data.map.title;
      } else if (this.selectData == "seller") {
        this.allData = data.seller.data;
        title = data.seller.title;
      } else if (this.selectData == "commodity") {
        this.allData = data.commodity.data;
        title = data.commodity.title;
      }
      var xData = data.common.month;
      var cityData = this.allData.map((city) => {
        return city.name;
      });
      let seriesData = [];
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];

      for (let i = 0; i < this.allData.length; i++) {
        let objs = {};
        objs.data = this.allData[i].data;
        objs.type = "line";
        objs.stack = "map";
        objs.name = this.allData[i].name;
        objs.areaStyle = {
          color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: colorArr1[i] },
            { offset: 1, color: colorArr2[i] },
          ]),
        };
        seriesData.push(objs);
      }

      //报表数据样式等设计
      var dataOptions = {
        title: {
          text: "▎ " + title,
          left: 20,
          top: 20,
          textStyle: {
            color: "#893448",
          },
        },
        legend: {
          data: cityData,
          top: "15%",
          icon: "circle",
          left: 20,
        },
        xAxis: {
          data: xData,
        },
        series: seriesData,
        grid: {
          top: "35%",
          left: "3%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
        },
      };
      this.myCharts.setOption(dataOptions);
    },
    // 当浏览器的大小发生变化的时候, 会调用的方法, 来完成屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.trend.offsetWidth / 100) * 3.6;
      if (titleFontSize < 15) {
        this.size = "mini";
      } else if (titleFontSize >= 15 && titleFontSize < 47) {
        this.size = "small";
      } else if (titleFontSize >= 47 && titleFontSize < 56) {
        this.size = "medium";
      }
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
      };
      this.myCharts.setOption(adapterOption);
      this.myCharts.resize();
    },
  },
  mounted: function () {
    this.initCharts();
    this.createReports();
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
};
</script>

<style>
.title {
  position: absolute;
  left: 20px;
  top: 22%;
  color: white;
  z-index: 10;
}
.com-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.com-chart {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>